<template>
	<!-- 加工件添加或编辑 -->
	<div>
		<v-dialog v-model="processModel.dialog" fullscreen transition="dialog-bottom-transition">
			<v-toolbar dark color="primary">
				<v-btn icon dark @click="processModel.dialog = false">
					<v-icon>mdi-close</v-icon>
				</v-btn>
				<v-toolbar-title>编辑加工件</v-toolbar-title>
				<v-spacer></v-spacer>
				<v-btn tile color="green" @click="submitAudit">提交审核</v-btn>
			</v-toolbar>
			<v-card>
				<v-card-title>
					<v-btn class="ml-2 mt-1" small tile color="primary" @click="addPartClick">
						添加部件
					</v-btn>
				</v-card-title>
				<v-card-text>
					<v-expansion-panels v-model="panel" multiple>
						<v-expansion-panel :key="0">
							<v-expansion-panel-header>设备信息</v-expansion-panel-header>
							<v-expansion-panel-content>
								<v-data-table :headers="processModel.equiDataHeaderList"
									:items="processModel.equiDataList" small fixed-header item-key="id" dense
									class="text-no-wrap" hide-default-footer>
								</v-data-table>
							</v-expansion-panel-content>
						</v-expansion-panel>

						<v-expansion-panel v-for="(item,i) in partItems" :key="'info-'+i"
							@click="setIsCommentToSelect(item)">
							<v-expansion-panel-header>
								<v-row>
									<v-col cols="12" sm="3" md="1">
										<span style="font-weight: bold;">部件信息</span>
									</v-col>
									<v-col cols="12" sm="3" md="2">
										<v-btn small tile color="primary" @click="addPartCommentClick(item)">
											添加零件
										</v-btn>
										<v-btn class="ml-1" small tile color="primary"
											@click="partDialogModel.partModel=item,partDialogModel.partDialog=true">
											修改部件
										</v-btn>
										<v-btn class="ml-1" small tile color="red" @click="deletePartSubmit(item.id)">
											删除部件
										</v-btn>
									</v-col>

									<v-col cols="12" sm="3" md="2">
										部件名称 : <a>{{item.name}}</a>
									</v-col>
									<v-col cols="12" sm="3" md="2">
										部件图号 : <a>{{item.piccode}}</a>
									</v-col>
									<v-col cols="12" sm="3" md="1">
										总数量 : <a>{{item.totalcount}}</a>
									</v-col>
									<v-col cols="12" sm="3" md="1">
										单套数量 : <a>{{item.singlecount}}</a>
									</v-col>
									<v-col cols="12" sm="3" md="1">
										单位 : <a>{{item.unit}}</a>
									</v-col>
									<v-col cols="12" sm="3" md="1">
										文件数量 : <a>{{item.fileInfos.length}}</a>
									</v-col>
									<v-col cols="12" sm="3" md="1">
										审核状态 : <a>{{item.approval==null?'': item.approval.approvalstatusname}}</a>
									</v-col>
								</v-row>
								
							</v-expansion-panel-header>
							<v-expansion-panel-content>
								<v-data-table :headers="partCommentDataHeaderList" :items="item.partCommentList" small
									fixed-header item-key="id" dense class="text-no-wrap" loading-text="正在加载请稍等......"
									:footer-props="vuetifyDefaultPage" show-expand single-expand>
									<template v-slot:item.actions="{ item }">
										<div>
											<v-btn class="ml-2" small tile color="primary"
												@click="partCommentModel=item,partCommentModel.notupdate=true,partCommentDialog=true">
												编辑
											</v-btn>
											<v-btn class="ml-2" small tile color="red"
												@click="deletePartCommentSubmit(item.id)">
												删除
											</v-btn>
										</div>
									</template>
									<template v-slot:item.iscomment="{ item }">
										<div>
											<a v-if="item.iscomment" style="color: #67C23A;">是</a>
											<a v-else>否</a>
										</div>
									</template>
									<template v-slot:item.filecount="{ item }">
										<div>
											<a>{{item.fileInfos.length}}</a>
										</div>
									</template>
								</v-data-table>

							</v-expansion-panel-content>
						</v-expansion-panel>
					</v-expansion-panels>

				</v-card-text>
				<v-card-actions></v-card-actions>
			</v-card>
		</v-dialog>
		<!-- 编辑部件对话框 -->
		<v-dialog v-model="partDialogModel.partDialog" width="1000">
			<v-card>
				<v-card-title>编辑部件</v-card-title>
				<v-card-text>
					<v-text-field v-model="partDialogModel.partModel.name" label="部件名称" outlined></v-text-field>
					<!-- <v-text-field v-model="partModel.partpicode" label="部件图号" disabled outlined></v-text-field> -->
					<v-text-field v-model="partDialogModel.partModel.totalcount" :rules="qtyRule" label="总数量" outlined>
					</v-text-field>
					<v-text-field v-model="partDialogModel.partModel.singlecount" :rules="qtyRule" label="单套数量"
						outlined>
					</v-text-field>
					<v-combobox v-model="partDialogModel.partModel.unit" :return-object="false" :items="unitItems"
						label="单位" outlined chips></v-combobox>
					<uploadfile :UpLoad="UpLoad" :callBackPath="partCallBackPath"></uploadfile>
				</v-card-text>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="primary" class="mr-4"
						:disabled="partDialogModel.partModel.totalcount<0||partDialogModel.partModel.singlecount<0"
						small tile @click="editPartSubmit">确定
					</v-btn>
					</v-btn>
					<v-btn color="warning" class="mr-4" small tile @click="closePart">取消</v-btn>

				</v-card-actions>
			</v-card>
		</v-dialog>
		<!-- 编辑零件对话框 -->
		<v-dialog v-model="partCommentDialog" width="1000">
			<v-card>
				<v-card-title>编辑零件</v-card-title>
				<v-card-text>
					<v-checkbox v-model="partCommentModel.iscomment" :disabled="partCommentModel.notupdate"
						label="是否是组件（组件不参与生产，多个零件组成一个组件)" dense>
					</v-checkbox>
					<v-text-field v-if="partCommentModel.iscomment" v-model="partCommentModel.partcommentname"
						label="组件名称" dense outlined>
					</v-text-field>
					<v-text-field v-else v-model="partCommentModel.name" label="零件名称" dense outlined></v-text-field>

					<v-select v-if="!partCommentModel.iscomment" v-model="partCommentModel.partcommentname"
						:items="CommentToSelectList" label="选择所属组件" dense outlined></v-select>

					<v-text-field v-if="partCommentModel.iscomment" v-model="partCommentModel.piccode"
						label="图号(组件需手动输入)" dense outlined>
					</v-text-field>

					<v-text-field v-model="partCommentModel.totalcount" :rules="qtyRule" dense label="总数量" outlined>
					</v-text-field>
					<v-text-field v-model="partCommentModel.singlecount" :rules="qtyRule" dense label="单套数量" outlined>
					</v-text-field>
					<v-text-field v-model="partCommentModel.surface_treatment" label="表面处理" outlined>
					</v-text-field>
					<v-text-field v-model="partCommentModel.process" label="工艺" outlined>
					</v-text-field>

					<v-row>
						<v-col cols="10">
							<v-text-field v-if="partCommentModel.id<=0" v-model="inventoryMode.cinvname" label="材料"
								outlined>
							</v-text-field>
							<v-text-field v-else v-model="partCommentModel.material" label="材料" outlined>
							</v-text-field>
						</v-col>
						<v-col cols="2">
							<v-btn class="mt-2" tile color="primary" @click="selectInventoryClick">选择存货</v-btn>
						</v-col>
					</v-row>

					<v-combobox v-model="partCommentModel.unit" :return-object="false" :items="unitItems" label="单位"
						dense outlined chips></v-combobox>
					<uploadfile :UpLoad="UpLoad" :callBackPath="partCommentCallBackPath"></uploadfile>

				</v-card-text>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="primary" class="mr-4"
						:disabled="partCommentModel.totalcount<0||partCommentModel.singlecount<0" small tile
						@click="editPartCommentSubmit">确定
					</v-btn>
					</v-btn>
					<v-btn color="warning" class="mr-4" small tile @click="closePartComment">取消</v-btn>

				</v-card-actions>
			</v-card>
		</v-dialog>

		<inventorycomment :materialComment="materialComment" :setMaterialcallBack="setMaterialcallBack"></inventorycomment>
	</div>

</template>

<script>
	import inventorycomment from '../InventoryDataList.vue'
	import uploadfile from '../../../mes/components/UploadFile.vue'
	export default {
		name: 'add_edit_mechanical_processing',
		components: {
			uploadfile,
			inventorycomment
		},
		props: {
			callBack: {
				type: Function, //参数类型：函数
			},
			processModel: {
				dialog: false,
				//选中项目设备信息
				selectEquiModel: {},
				equiDataHeaderList: [],
				equiDataList: [],
			},
			//部件
			partDialogModel: {
				partDialog: false,
				partModel: {}
			},
		},
		data: () => ({
			panel: [0],
			qtyRule: [
				v => /^[0-9]*$/.test(v) || '请输入合法数字',
				v => v >= 0 || '需求量不能小于0'
			],

			partItems: [],
			unitItems: [],
			//零件
			partCommentDataHeaderList: [],
			partCommentDataList: [],
			vuetifyDefaultPage: {
				'items-per-page-text': '每页个数',
				'items-per-page-options': [10, 20, 50, 100]
			},
			partCommentDialog: false,
			partCommentModel: {},
			CommentToSelectList: [],
			UpLoad: {
				isUploadSingleImage: false,
				isUploadMultipleFiles: true,
				type:40
			},
			materialComment: {
				dialog: false,
				loadInventoryPageUrl: ``,
				loadInventoryClassUrl: ``,
				loadUnitsUrl: ``,
				queryInvClassTreeUrl: `PDM/PDMDataMechanical/GetInvclassTree?iinvcgrade=`
			},
			inventoryMode: {}
		}),
		watch: {
			processModel: {
				handler(newVal) {
					this.$emit('update:processModel', newVal)
					this.loadParts()
				},
				deep: true
			},
			partDialogModel: {
				handler(newVal) {
					this.$emit('update:partDialogModel', newVal)
					if (!this.partDialogModel.partDialog) {
						return
					}
					this.loadUnits()
				},
				deep: true
			},
		},
		created() {
			this.getHeaderList()
		},
		methods: {
			async loadUnits() {
				var response = await this.$httpRequest({
					url: `PDM/PDMDataMechanical/loadUnits`,
					method: 'get',
				})
				if (response.status == 1) {
					this.unitItems = response.data
				}
			},
			async loadParts() {
				if (!this.processModel.dialog) {
					return
				}
				await this.loadUnits()
				var response = await this.$httpRequest({
					url: `PDM/PDMDataMechanical/LoadPartList?equiId=${this.processModel.selectEquiModel.id}`,
					method: 'get',
				})
				if (response.status == 1) {
					this.partItems = response.data
					this.partItems.forEach(r => {
						if (r.partCommentList.length > 0 && r.partCommentList.filter(c => c.iscomment).length >
							0) {

							r.CommentToSelectList = r.partCommentList.filter(c => c.iscomment).map(
								c => {
									return {
										text: c.partcommentname,
										value: c.partcommentname
									}
								})
						}
					})
				}
			},
			//打开添加部件对话框
			addPartClick() {
				this.partDialogModel.partModel = {}
				this.partDialogModel.partModel.fileInfos = []
				this.partDialogModel.partDialog = true
				this.partDialogModel.partModel.equipmentid = this.processModel.selectEquiModel.id
				this.partDialogModel.partModel.projectid = this.processModel.selectEquiModel.projectid
			},
			//提交部件信息
			async editPartSubmit() {
				var url;
				if (this.partDialogModel.partModel.id > 0) {
					url = `PDM/PDMDataMechanical/UpdatePart`
				} else {
					url = `PDM/PDMDataMechanical/AddPart`
				}
				// this.Model.projectid = this.equiModel.projectId
				var response = await this.$httpRequest({
					url: url,
					method: 'post',
					data: this.partDialogModel.partModel
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					this.closePart()
					await this.loadParts()
				} else {
					this.$message.error(response.info)
				}
			},
			async deletePartSubmit(partId) {
				var response = await this.$httpRequest({
					url: `PDM/PDMDataMechanical/DeletePart?partId=${partId}`,
					method: 'get',
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					await this.loadParts()
				} else {
					this.$message.error(response.info)
				}
			},
			//关闭部件对话框
			closePart() {
				this.partDialogModel.partModel = {}
				this.partDialogModel.partModel.fileInfos = []
				this.partDialogModel.partDialog = false
			},
			addPartCommentClick(part) {
				this.partCommentModel = {}
				this.partCommentDialog = true
				this.partCommentModel.id = 0
				this.partCommentModel.notupdate = false
				this.partCommentModel.equipmentid = this.processModel.selectEquiModel.id
				this.partCommentModel.projectid = this.processModel.selectEquiModel.projectid
				this.partCommentModel.partid = part.id
			},
			setIsCommentToSelect(item) {
				this.CommentToSelectList = item.CommentToSelectList
			},
			//零件
			async editPartCommentSubmit() {
				var url;
				if (this.partCommentModel.id > 0) {
					url = `PDM/PDMDataMechanical/UpdatePartComment`
				} else {
					url = `PDM/PDMDataMechanical/AddPartComment`
				}
				// this.Model.projectid = this.equiModel.projectId
				var response = await this.$httpRequest({
					url: url,
					method: 'post',
					data: this.partCommentModel
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					this.closePartComment()
					await this.loadParts()
				} else {
					this.$message.error(response.info)
				}
			},
			async deletePartCommentSubmit(partCommentId) {
				var response = await this.$httpRequest({
					url: `PDM/PDMDataMechanical/DeletePartComment?partCommentId=${partCommentId}`,
					method: 'get',
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					await this.loadParts()
				} else {
					this.$message.error(response.info)
				}
			},
			closePartComment() {
				this.partCommentModel = {}
				this.partCommentModel.fileInfos = []
				this.partCommentDialog = false
			},
			//部件文件上传回调
			partCallBackPath(url) {
				this.partDialogModel.partModel.fileInfos = url
			},
			partCommentCallBackPath(url) {
				this.partCommentModel.fileInfos = url
			},
			selectInventoryClick() {
				this.materialComment.dialog = true
				this.materialComment.loadInventoryPageUrl = `PDM/PDMDataMechanical/LoadInventoryPage`
				this.materialComment.loadInventoryClassUrl = `PDM/PDMDataMechanical/loadInventoryClass`
				this.materialComment.loadUnitsUrl = `PDM/PDMDataMechanical/loadUnits`
			},
			//选中的存货回调
			setMaterialcallBack(inventory) {
				this.inventoryMode = inventory
				this.partCommentModel.cinvcode = inventory.cinvcode
				this.partCommentModel.material = inventory.cinvname
				this.partCommentModel.specifications = inventory.cinvstd
				this.partCommentModel.unit = this.getDataListUnit(inventory.ccomunitcode)
			},
			getDataListUnit(unitcode) {
				var items = this.unitItems.filter(r => r.unitcode == unitcode)
				if (items != null && items.length > 0) {
					return items[0].text
				}
			},
			//提交审核
			async submitAudit() {
				var response = await this.$httpRequest({
					url: `PDM/PDMDataMechanical/SubmitAudit?equiId=${this.processModel.selectEquiModel.id}`,
					method: 'get'
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					this.processModel.dialog = false
				} else {
					this.$message.error(response.info)
				}

			},
			getHeaderList() {
				this.partCommentDataHeaderList = [{
						text: '操作',
						value: 'actions',
						align: 'center',
						filterable: true,
					},
					{
						text: '是否组件',
						value: 'iscomment',
						align: 'center',
						filterable: true,
					},
					{
						text: '加工件编码',
						value: 'code',
						align: 'left',
						filterable: true,
					},
					{
						text: '部件名称',
						value: 'partname',
						align: 'left',
						filterable: true,
					},
					{
						text: '组件名称',
						value: 'partcommentname',
						align: 'left',
						filterable: true,
					},
					{
						text: '零件名称',
						value: 'name',
						align: 'left',
						filterable: true,
					},
					{
						text: '图号',
						value: 'piccode',
						align: 'left',
						filterable: true,
					},
					{
						text: '总数量',
						value: 'totalcount',
						align: 'center',
						filterable: true,
					},
					{
						text: '单套数量',
						value: 'singlecount',
						align: 'left',
						filterable: true,
					},
					{
						text: '存货编号',
						value: 'cinvcode',
						align: 'left',
						filterable: true,
					},
					{
						text: '材料',
						value: 'material',
						align: 'left',
						filterable: true,
					},
					{
						text: '规格型号',
						value: 'specifications',
						align: 'left',
						filterable: true,
					},
					{
						text: '单位',
						value: 'unit',
						align: 'center',
						filterable: true,
					},
					{
						text: '表面处理',
						value: 'surface_treatment',
						align: 'center',
						filterable: true,
					},
					{
						text: '工艺',
						value: 'process',
						align: 'center',
						filterable: true,
					},
					{
						text: '文件数量',
						value: 'filecount',
						align: 'center',
						filterable: true,
					},
				]
			},
		},
	}
</script>

<style>
</style>
